package myweb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;





















import lr.com.demo.dao.impl.RedisDaoImpl;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.data.redis.serializer.RedisSerializer;

import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;


public class test {
	RedisTemplate<String, String> redisTemplate;
	private RedisDaoImpl redisDao=null;  
	@Before
	public void myInit(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		redisTemplate = (RedisTemplate<String, String>) applicationContext.getBean("redisTemplate");
        redisDao = (RedisDaoImpl) applicationContext.getBean("redisDao");  
    }  
	@Test  
    public void testPublishMessage() throws Exception {  
        String msg = "和哈哈哈哈哈哈和";  
        redisDao.sendMessage("java", msg); //发布字符串消息  
	}
	@Test
	public void saveString(){
		String name;
		boolean flag;
		ValueOperations<String, String> ops = redisTemplate.opsForValue();
		ops.set("name", "lirui");
		ops.set("age", "10");
		name = ops.get("name", 0,2 );//对应  getrange命令
		System.err.println(name);
		ops.append("name", "7721");
		name = ops.get("name");
		System.err.println(name);
		
		/*List<String> keys = new ArrayList<String>();
		keys.add("name");
		keys.add("age");
		List<String> vals = ops.multiGet(keys); // 对应  mget
		for(String st:vals){
			System.err.println(st);
		}*/
		/*flag = ops.setIfAbsent("sex", "1");// 对应 setnx
		System.err.println(flag);*/
		/*ops.increment("age", 5); //对应  incrby 
		System.err.println(ops.get("age"));*/
		
		
		
		RedisOperations<String, String> obj = ops.getOperations();
		/*Set<String> o = obj.keys("*"); // 对应 keys *
		Iterator<String> its = o.iterator();
		while(its.hasNext()){
			System.err.println(its.next());
		}*/
		/*System.err.println(obj.randomKey());//randomKey */
	}
	@Test
	public void testHash(){
		HashOperations<String, Object, Object> hops = redisTemplate.opsForHash();
		 //对应  hset hmget
		/*hops.put("user:001", "name", "lirui");  
		hops.put("user:001", "age", "30");
		hops.put("user:001", "sex", "0");
		List<Object> ks = new ArrayList<Object>();
		ks.add("name");
		ks.add("age");
		ks.add("sex");
		List<Object> vs = hops.multiGet("user:002", ks);
		for(Object v:vs){
			System.err.println(v);
		}
		*/
		// hmset hmget 命令
		/*Map<String, String> map = new HashMap<String, String>();
		map.put("name", "lr");
		map.put("age", "20");
		map.put("sex", "1");
		hops.putAll("user:002", map);
		List<Object> ks = new ArrayList<Object>();
		ks.add("name");
		ks.add("age");
		ks.add("sex");
		List<Object> vs = hops.multiGet("user:002", ks);
		for(Object v:vs){
			System.err.println(v);
		}*/
		
		// hkeys 获取hash表的字段
 		/*Set<Object> keys = hops.keys("user:001");
		Iterator<Object> its = keys.iterator();
		while(its.hasNext()){
			System.err.println(its.next());
		}*/
		//hvals 
		/*List<Object> obj = hops.values("user:001");
		for(Object o:obj){
			System.err.println(o);
		}*/
	}
	@Test
	public void testList(){
		ListOperations<String, String> ops = redisTemplate.opsForList();
		List<String> obj;
		// leftPush 对应 lpush  头  rightpush 对应  rpush 尾
		/*ops.leftPush("list1", "lllll");
		ops.leftPush("list1", "lrlrlr");
		System.err.println(ops.leftPop("list1"));*/
		/*ops.leftPush("list2", "a");
		ops.leftPush("list2","a" , "b"); //对应  linsert 
		System.err.println(ops.leftPop("list2"));*/
		/*ops.leftPush("list3", "a");
		ops.leftPush("list3", "a");
		ops.leftPush("list3", "a");
		ops.rightPush("list3", "b");
		obj = ops.range("list3", 0, -1);
		for(String s:obj){
			System.err.println(s);
		}
		System.err.println("-------分割线---------");
		ops.remove("list3", 2, "a");	
		obj = ops.range("list3", 0, -1);
		for(String s:obj){
			System.err.println(s);
		}*/
		/*ops.leftPush("list4", "a");
		ops.leftPush("list5", "b");
		ops.rightPopAndLeftPush("list4", "list5");
		obj = ops.range("list5", 0, -1);
		for(String s : obj){
			System.err.println(s);
		}*/
		/*Long a = ops.size("list5");
		System.err.println(a);*/
		/*ops.set("list5", 1, "c");// lset
		obj = ops.range("list5", 0, -1);
		for(String s : obj){
			System.err.println(s);
		}*/
		/*String str = ops.index("list5", 0);//lindex 
		System.err.println(str);*/
	}
	@Test
	public void addSetData(){
		SetOperations<String, String> ops = redisTemplate.opsForSet();
		ops.add("set1", "1");
		ops.add("set1", "2");
		ops.add("set1", "3");
		ops.add("set2", "2");
		ops.add("set2", "3");
		ops.add("set2", "4");
		ops.add("set2", "5");
	}
	@Test
	public void testSet(){
		SetOperations<String, String> ops = redisTemplate.opsForSet();
		Set<String> obj = new HashSet<String>();
		//smember
		/*obj = ops.members("set1");*/
		
		//srandmember  
		/*System.err.println(ops.randomMember("set1"));*/
		//sismember 
		/*System.err.println(ops.isMember("set1", "123"));
		System.err.println(ops.isMember("set1", "1"));*/
		//sdiff  以前面的集合为标准  返回前面集合的数据
		/*obj = ops.difference("set1", "set2");
		ops.differenceAndStore("set1", "set2", "set3");
		obj = ops.members("set3"); */
		//sinter  
		/*obj = ops.intersect("set1", "set2");
		ops.intersectAndStore("set1", "set2", "set4");
		obj = ops.members("set4");*/
		//sunion  
		/*obj = ops.union("set1", "set2");
		ops.unionAndStore("set1", "set2", "set5");
		obj = ops.members("set5");*/
		Iterator<String> it = obj.iterator();
		while(it.hasNext()){
			System.err.println(it.next());
		}
	}
	@Test
	public void addZsetData(){
		ZSetOperations<String, String> ops = redisTemplate.opsForZSet();
		ops.add("zset1", "a", 1);
		ops.add("zset1","b", 2);
		ops.add("zset1", "c", 3);
		ops.add("zset1", "d", 4);
		ops.add("zset1", "e", 5);
		ops.add("zset2", "d", 1);
		ops.add("zset2", "e", 2);
		ops.add("zset2", "f", 3);
		ops.add("zset2", "g", 4);
		ops.add("zset2", "h", 5);
	}
	@Test
	public void testZset(){
		ZSetOperations<String, String> ops = redisTemplate.opsForZSet();
		Set<TypedTuple<String>> obj = new HashSet<TypedTuple<String>>();
		Set<String> obj1  = new HashSet<String>();
		//zrevrange myzset 0 -1 withscores 
		/*obj = ops.rangeWithScores("zset1", 0,-1);
		*/
		//zrevrange
		/*obj1 = ops.range("zset1",0 , -1);
		*/
		//zrangebyscore  
		/*obj1 = ops.rangeByScore("zset1", 0, 10);*/
		//交集
		/*ops.intersectAndStore("zset1", "zset2", "zset3");
		obj1 = ops.range("zset3", 0, -1);*/
		//zrank 返回元素下标值
		/*Long a = ops.rank("zset1", "b");
		System.err.println(a);*/
		//zremrangebyrank   删除给定下标区间的 元素
		/*System.err.println(ops.removeRange("zset1", 1, 3));
		obj1 = ops.range("zset1",0 , -1);*/
		//zremrangebyscore  删除给定序号区间的元素
		/*ops.removeRangeByScore("zset2", 1, 3);
		obj1 = ops.range("zset2",0 , -1);*/
		Iterator<TypedTuple<String>> it = obj.iterator();
		while(it.hasNext()){
			TypedTuple<String> z = it.next();
			System.err.println(z.getValue()+"-----"+z.getScore().intValue());
		}
		Iterator<String> it1 = obj1.iterator();
		while(it1.hasNext()){
			System.err.println(it1.next());
		}
		
	}

}
